package ua.mykola.dynamicp;

public class LongestCommonSubsequence
{

    public int length(String a, String b)
    {
        return length(a, a.length() - 1, b, b.length() - 1);
    }

    private int length(String a, int indexA, String b, int indexB)
    {
        if (indexA == -1 || indexB == -1)
            return 0;

        if (a.charAt(indexA) == b.charAt(indexB))
        {
            return length(a, indexA - 1, b, indexB - 1) + 1;
        }
        else
        {
            return Math.max(length(a, indexA, b, indexB - 1), length(a, indexA - 1, b, indexB));
        }
    }
}
